package org.baiduai.face;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Base64;
import java.util.HashMap;


/**
 * 人脸检测与属性分析
 */
public class FaceDetect {

    private static final String APP_ID = "16690731";
    private static final String API_KEY = "4F41W3Vy6AqKmu0snN4Iqu6y";
    private static final String SECRET_KEY = "14SpxTBafwMFNKvYmLMkBRX3D0BYSaoL";

    public static void auth() {

        //    AipFace face = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        //    ImageUtil.getImageFormatByFile(filePath)
        //    face.detect(image, imageType, null);

    }

    public static void main(String[] args) throws IOException {

        String filePath = "C:\\Users\\naibi\\Desktop\\1111.jpg";

        InputStream in = new FileInputStream(filePath);
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = in.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }

        String image = Base64.getEncoder().encodeToString(swapStream.toByteArray());
        in.close();
        //  System.out.println("=========== >>>> " + image);

        //  System.out.println("=========== >>>> ");

        AipFace face = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        HashMap<String, String> options = new HashMap<String, String>();

        // 添加 选项
        options.put("face_field", "age,beauty,expression,face_type");

        JSONObject json = face.detect(image, "BASE64", options);

        System.out.println(json);

    }
}